﻿using FlyingEye.Users.Models;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace FlyingEye.EntityFrameworkCore
{
#if DEBUG
    //[ConnectionStringName("LocalMySql")]
    [ConnectionStringName("TestMySql")]
#endif
#if RELEASE
    [ConnectionStringName("MySql")]
#endif
    public class MySqlDbContext : AbpDbContext<MySqlDbContext>
    {
        public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
        {
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        public DbSet<UserModel>? Users { get; set; }

        /// <summary>
        /// 申请创建用户的工单列表
        /// </summary>
        public DbSet<UserCreationOrderModel>? UserCreationOrders { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<UserModel>(builder =>
            {
                builder.ToTable(MySqlConsts.DbTablePrefix + "Users", MySqlConsts.DbSchema)
                .Property((x => x.UserName)).IsRequired().HasMaxLength(128);
                builder.Property(x => x.Password).IsRequired().HasMaxLength(1024);
            });

            modelBuilder.Entity<UserCreationOrderModel>(builder =>
            {
                builder.ToTable(MySqlConsts.DbTablePrefix + "UserCreationOrders", MySqlConsts.DbSchema)
                .Property((x => x.UserName)).IsRequired().HasMaxLength(128);
                builder.Property(x => x.Password).IsRequired().HasMaxLength(1024);
            });
        }
    }
}
